package com.ojoin.trade.supplyloan_core.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlSupplyInventoryDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;
import com.ojoin.trade.supplyloan_core.web.service.TbCatchdataStrategyService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformInfoService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformUserService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyInventoryService;

@Service
public class TbSlSupplyInventoryServiceImpl extends
		BaseServiceImpl<TbSlSupplyInventory> implements
		TbSlSupplyInventoryService {
	private Logger log = LoggerFactory
			.getLogger(TbSlSupplyInventoryServiceImpl.class);
	@Autowired
	private TbSlSupplyInventoryDao dao;

	@Autowired
	private TbSlPlatformInfoService platformInfoService;

	@Autowired
	private TbSlPlatformUserService platformUserService;
	
	@Autowired
	private TbCatchdataStrategyService catchdataStrategyService;
	
	@Override
	protected TbSlSupplyInventoryDao getBaseDao() {
		return dao;
	}

	@Override
	public List<TbSlSupplyInventory> queryInventoryByRowCreateTime(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd) {
		return queryInventoryByRowCreateTime(platformAccountId,
				rowCreateTimeStart, rowCreateTimeEnd, null);
	}

	@Override
	public List<TbSlSupplyInventory> queryInventoryByRowCreateTime(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd, String itemStatus) {
		return queryInventoryByRowCreateTime(platformAccountId,
				rowCreateTimeStart, rowCreateTimeEnd, itemStatus, null);
	}

	@Override
	public List<TbSlSupplyInventory> queryInventoryByRowCreateTime(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd, String itemStatus, Pageable page) {
		return this.getBaseDao().queryByRowCreateTime(platformAccountId,
				rowCreateTimeStart, rowCreateTimeEnd, itemStatus, page);
	}

	@Override
	public List<TbSlSupplyInventory> queryInventoryByStoreDate(
			int platformAccountId, String storeDateStart, String storeDateEnd) {
		return queryInventoryByStoreDate(platformAccountId, storeDateStart,
				storeDateEnd, null);
	}

	@Override
	public List<TbSlSupplyInventory> queryInventoryByStoreDate(
			int platformAccountId, String storeDateStart, String storeDateEnd,
			String itemStatus) {
		return queryInventoryByStoreDate(platformAccountId, storeDateStart,
				storeDateEnd, itemStatus, null);
	}

	@Override
	public List<TbSlSupplyInventory> queryInventoryByStoreDate(
			int platformAccountId, String storeDateStart, String storeDateEnd,
			String itemStatus, Pageable page) {
		return this.getBaseDao().queryByStoreDate(platformAccountId,
				storeDateStart, storeDateEnd, itemStatus, page);
	}

	@Override
	public Map<String, TbSlSupplyInventory> queryInventoryInByStoreDateMap(
			int platformAccountId, String storeDateStart, String storeDateEnd) {
		return queryInventoryByStoreDateMap(platformAccountId, storeDateStart,
				storeDateEnd, TbSlSupplyInventory.IN);
	}

	@Override
	public Map<String, TbSlSupplyInventory> queryInventoryOutByStoreDateMap(
			int platformAccountId, String storeDateStart, String storeDateEnd) {
		return queryInventoryByStoreDateMap(platformAccountId, storeDateStart,
				storeDateEnd, TbSlSupplyInventory.OUT);
	}

	@Override
	public Map<String, TbSlSupplyInventory> queryInventoryByStoreDateMap(
			int platformAccountId, String storeDateStart, String storeDateEnd) {
		return queryInventoryByStoreDateMap(platformAccountId, storeDateStart,
				storeDateEnd, null);
	}

	@Override
	public Map<String, TbSlSupplyInventory> queryInventoryByStoreDateMap(
			int platformAccountId, String storeDateStart, String storeDateEnd,
			String itemStatus) {
		Map<String, TbSlSupplyInventory> map = null;
		List<TbSlSupplyInventory> li = queryInventoryByStoreDate(
				platformAccountId, storeDateStart, storeDateEnd, itemStatus);
		if (null != li && li.size() > 0) {
			map = new HashMap<String, TbSlSupplyInventory>();
			for (int i = 0; i < li.size(); i++) {
				TbSlSupplyInventory tbsl = li.get(i);
				map.put(tbsl.getItemKey(), tbsl);

			}
		}

		return map;
	}

	@Override
	public long queryInventoryInMoney(int platformAccountId) {
		return queryInventoryInMoneyByStoreDate(platformAccountId, null, null);
	}

	@Override
	public long queryInventoryOutMoney(int platformAccountId) {
		return queryInventoryOutMoneyByStoreDate(platformAccountId, null, null);
	}

	@Override
	public long queryInventoryInMoneyByStoreDate(int platformAccountId,
			String storeDateStart, String storeDateEnd) {
		return queryInventoryMoneyByStoreDate(platformAccountId,
				storeDateStart, storeDateEnd, TbSlSupplyInventory.IN);
	}

	@Override
	public long queryInventoryOutMoneyByStoreDate(int platformAccountId,
			String storeDateStart, String storeDateEnd) {
		return queryInventoryMoneyByStoreDate(platformAccountId,
				storeDateStart, storeDateEnd, TbSlSupplyInventory.OUT);
	}

	@Override
	public long queryInventoryMoneyByStoreDate(int platformAccountId,
			String storeDateStart, String storeDateEnd, String itemStatus) {
		return this.getBaseDao().queryInventoryMoneyByStoreDate(
				platformAccountId, storeDateStart, storeDateEnd, itemStatus);
	}

	@Override
	public long queryInventoryInMoneyByRowCreateTime(int platformAccountId,
			String rowCreateTimeStart, String rowCreateTimeEnd) {
		return queryInventoryMoneyByRowCreateTime(platformAccountId,
				rowCreateTimeStart, rowCreateTimeEnd, TbSlSupplyInventory.IN);
	}

	@Override
	public long queryInventoryOutMoneyByRowCreateTime(int platformAccountId,
			String rowCreateTimeStart, String rowCreateTimeEnd) {
		return queryInventoryMoneyByRowCreateTime(platformAccountId,
				rowCreateTimeStart, rowCreateTimeEnd, TbSlSupplyInventory.OUT);
	}

	@Override
	public long queryInventoryMoneyByRowCreateTime(int platformAccountId,
			String rowCreateTimeStart, String rowCreateTimeEnd,
			String itemStatus) {
		return this.getBaseDao().queryInventoryMoneyByRowCreateTime(
				platformAccountId, rowCreateTimeStart, rowCreateTimeEnd,
				itemStatus);
	}

	@Override
	public int delete(Integer platformAccountId, String rowCreateTime) {
		return getBaseDao().deleteByPlatformAccountId(platformAccountId,
				rowCreateTime);
	}

	@Override
	public int delete(Integer platformAccountId) {
		return delete(platformAccountId, null);
	}

	@Override
	public void deleteRepeat(int platformAccountId) {
		this.getBaseDao().deleteRepeat(platformAccountId);
	}

	@Override
	public String getLastInsertTime(int platformAccountId) {
		return dao.getLastInsertTime(platformAccountId);
	}

	@Override
	public Map<String, TbSlSupplyInventory> queryInventoryMap(
			int platformAccountId) {
		return queryInventoryMap(platformAccountId, null);
	}

	@Override
	public Map<String, TbSlSupplyInventory> queryInventoryMap(
			int platformAccountId, String itemStatus) {
		return queryInventoryByStoreDateMap(platformAccountId, null, null,
				itemStatus);

	}

	@Override
	public void removeInventoryCommon(List<TbSlSupplyInventory> list,
			Map<String, TbSlSupplyInventory> MapDB) {

		if (!CommonUtils.isEmpty(MapDB)) {

			if (!CommonUtils.isEmpty(list)) {
				Set<String> set = MapDB.keySet();
				LogUtils.info(log, "库存数据:" + list.size());

				Iterator<TbSlSupplyInventory> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyInventory payment = it.next();
					if (set.contains(payment.getItemKey())) {

						it.remove();

						MapDB.remove(payment.getItemKey());

						LogUtils.info(log, "库存数据:" + payment.getItemKey()
								+ "已存在");

					}
				}
			}

		} else {
			LogUtils.info(log, "库存数据:" + list.size());

			Iterator<TbSlSupplyInventory> it = list.iterator();
			while (it.hasNext()) {
				TbSlSupplyInventory payment = it.next();

				LogUtils.info(log, "库存数据:" + payment.getItemKey() + "已存在");

			}
		}

	}

	@Override
	public List<TbSlSupplyInventory> changeToPay(
			Map<String, TbSlSupplyInventory> MapDB) {
		if (null != MapDB) {
			Iterator<TbSlSupplyInventory> it = MapDB.values().iterator();
			while (it.hasNext()) {
				TbSlSupplyInventory tb = it.next();
				tb.setItemStatus(TbSlSupplyInventory.PAY);

				LogUtils.info(log, "库存单据:" + tb.getItemKey() + "状态更新"
						+ TbSlSupplyInventory.PAY);
			}
			
			return new ArrayList<TbSlSupplyInventory>(MapDB.values());
		}

		return null;
	}

	@Override
	public String getLastStoreDate(int platformAccountId) {
		return getLastStoreDate(platformAccountId,null);
	}

	@Override
	public String getLastStoreDate(int platformAccountId, String itemStatus) {
		return dao.getLastStoreDate(platformAccountId,itemStatus);
	}

	@Override
	public long queryInventoryMoney(int platformAccountId,
			String storeDateStart, String storeDateEnd,
			String rowCreateTimeStart, String rowCreateTimeEnd,
			String itemStatus) {
		return dao.queryInventoryMoney(platformAccountId, storeDateStart, storeDateEnd, rowCreateTimeStart, rowCreateTimeEnd, itemStatus);
	}
	
	@Override 
	public Map<Integer,List<TbSlSupplyInventory>> queryByMerchantUser(int idOfTbMerchantUser,
			String DateStart, String DateEnd,String settlementState) {
		// TODO Auto-generated method stub
		Map<Integer,List<TbSlSupplyInventory>> payments = new HashMap<Integer,List<TbSlSupplyInventory>>();
		List<TbSlPlatformUser> users = platformUserService.queryTbSlPlatformUserByJoinid(idOfTbMerchantUser);
		
		if(!CommonUtils.isEmpty(users)){
			Iterator<TbSlPlatformUser> it =  users.iterator();
			TbSlPlatformUser user =it.next();
			TbCatchdataStrategy ca = catchdataStrategyService.queryByPlatformUserId(user.getId());
			
			
			if(ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)){
				String time = TimeUtils.getCurDate();
				List<TbSlSupplyInventory> li = this.queryInventory(user.getId(), time, time, DateStart, DateEnd, settlementState, null);
			    payments.put(user.getId(), li);
			}else {
				List<TbSlSupplyInventory> li = this.queryInventoryByStoreDate(user.getId(), DateStart, DateEnd, settlementState);
				  payments.put(user.getId(), li);
			}
		}

		return payments;
	}

	@Override
	public List<TbSlSupplyInventory> queryInventory(int platformAccountId,
			String rowCreateTimeStart, String rowCreateTimeEnd,
			String storeDateStart, String storeDateEnd, String itemStatus,
			Pageable page) {
		return dao.query(platformAccountId, rowCreateTimeStart, rowCreateTimeEnd, storeDateStart, storeDateEnd, itemStatus, page);
	}
}
